package gameSite;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import gameSite.RemoteConnection;

/**
 * Servlet implementation class AdminLoginVerification
 */
@WebServlet("/AdminLoginVerification")
public class AdminLoginVerification extends HttpServlet {
	Connection conn;
	Statement stmt = null;
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLoginVerification() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.sendError(1, "Invalid request");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username").toString();
		String password = request.getParameter("password").toString();
		//if(verifyCredentials(username, password)){
			response.sendRedirect("DashboardValues");
		//}
		//else
			//response.sendRedirect("Invalid.jsp");
	}
	public boolean verifyCredentials(String username, String password){
		boolean login = false;
		try{
			//STEP 4: Execute a query
		      System.out.println("Creating statement...");
		      stmt = RemoteConnection.getConnection().createStatement();
		      String sql = "SELECT username FROM Manager where username='"+username+"' and password='"+password+"'";
		      ResultSet rs = stmt.executeQuery(sql);
		      
		      if(rs.next()){		        		  
		         //Display values
		         System.out.print("User: " + rs.getString("username") + " exists");
		         login = true;
		      }
		      rs.close();
		      stmt.close();
		      RemoteConnection.closeConnection();
		      return login;
		     
		}
		catch(SQLException se){
		      //Handle errors for JDBC
		      se.printStackTrace();
		   }
	      return false;
	}
}
